<?php

    // <login>
    function CreateLoginXML($slogin, $spassword)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $auth = $doc->createElement("authentification");
        //$text = $doc->createTextNode('Description');
        $auth->setAttribute('login', $slogin);
        $auth->setAttribute('password', $spassword);
        $doc->appendChild($auth);
        return $doc->SaveXML();
    }

    // <getdictionaries>
    function CreateGetDictionariesXML($datebegin, $dateend, $lpu_guid = null)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("getdictionaries");
        $root->setAttribute('datebegin', $datebegin);
        $root->setAttribute('dateend', $dateend);
        $root->setAttribute('lpu_guid', $lpu_guid);
        $doc->appendChild($root);
        return $doc->SaveXML();
    }
    
    /**
    * Процедура получения списка ЛПУ для конкретного человека
    * 
    * @param string $patientid Уникальный идентификатор пациента PatientId из «Витакарта-3»
    */
    function getLPUListXML($patientid)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("getlpulist");
        $root->setAttribute('patientid', $patientid);
        $doc->appendChild($root);        
        return $doc->SaveXML();
    }

    // <CreateCheckPersonXML>
    function CreateCheckPersonXML($omsseries, $omsnumber, $birthday, $lastname, $firstname, $fathername)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("checkperson");
        $root->setAttribute('omsseries', $omsseries);
        $root->setAttribute('omsnumber', $omsnumber);
        $root->setAttribute('birthday', $birthday);
        $root->setAttribute('lastname', $lastname);
        $root->setAttribute('firstname', $firstname);
        $root->setAttribute('fathername', $fathername);
        $doc->appendChild($root);        
        return $doc->SaveXML();
    }

    function CreateGetResourceScheduleXML($speciality_codes, $datebegin, $dateend)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("getresourceschedule");
        $root->setAttribute('datebegin', $datebegin);
        $root->setAttribute('dateend', $dateend);
        foreach($speciality_codes as $code)
        {
            $speciality = $doc->createElement("speciality");   
            $speciality->setAttribute('code', $code);
            $root->appendChild($speciality);
        }
        $doc->appendChild($root);        
        return $doc->SaveXML();
    }

    // <CreateGetScheduleXML>
    function CreateGetScheduleXML($speciality_codes, $datebegin, $dateend)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("getposts");
        $root->setAttribute('datebegin', $datebegin);
        $root->setAttribute('dateend', $dateend);
        foreach($speciality_codes as $code)
        {
            $speciality = $doc->createElement("speciality");   
            $speciality->setAttribute('code', $code);
            $root->appendChild($speciality);
        }
        $doc->appendChild($root);        
        return $doc->SaveXML();
    }

    // <remoterecord>
    function CreateRemoterecordXML($date, $patient_info, $tickets)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("remotedirection");
        $root->setAttribute('patient', $patient_info['uniqueid']);
        $root->setAttribute('target_date', $patient_info['target_date']);
        $root->setAttribute('direction_perpose', $patient_info['direction_perpose']);
        $root->setAttribute('director', $patient_info['director']);
        unset($patient_info['target_date'], $patient_info['direction_perpose'], $patient_info['director']);
        $patient = $doc->createElement("patient");
        $records = $doc->createElement("entries");
        foreach($patient_info as $key => $value)
        {
            if(is_array($value))
            {
                foreach($value as $key2 => $value2)
                {
                    if(!is_array($value2)){
                    $patient->setAttribute($key2, cnv_to($value2));}
                }
            }
            else
            {
                $patient->setAttribute($key, cnv_to($value));
            }
        }
        foreach($tickets as $ticket_code)
        {
            $record = $doc->createElement("entry");   
            $record->setAttribute('code', $ticket_code);
            $records->appendChild($record);
        }
        $root->appendChild($patient);
        $root->appendChild($records);
        $doc->appendChild($root);
        return $doc->SaveXML();
    }

    function CreateGetRemoteDirectionsXML($remotedirection = null, $method = "part", $month, $year, $persons = Array())
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("getremotedirections");
        $root->setAttribute('method', $method);
        $root->setAttribute('month', $month);
        $root->setAttribute('year', $year);
        $ps = $doc->createElement("persons");
        /* убрать */
            $p = $doc->createElement("person");
            $p->setAttribute('guid', "760CEF1A-1CA8-4bfd-9B4D-BA9A46394340");
            $ps->appendChild($p);     
        /* до сюда */
        foreach($persons as $person)
        {
            $p = $doc->createElement("person");
            $p->setAttribute('guid', $person);
            $ps->appendChild($p);     
        }
        $root->appendChild($ps);
        if($remotedirection != null)
        {
            $speciality = $doc->createElement("remotedirection");   
            $speciality->setAttribute('guid', $remotedirection);
            $root->appendChild($speciality);
        }
        $doc->appendChild($root);
        return $doc->SaveXML();  
    }

    function CreateDeleteRemoteDirectionXML($remotedirection)
    {
        $doc = new DOMDocument();
        $doc->formatOutput = true;
        $root = $doc->createElement("deleteremotedirection");
        $root->setAttribute('guid', $remotedirection);
        $doc->appendChild($root);        
        return $doc->SaveXML();  
    }

?>